package com.lnking.lnkingplace.controller;


import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.IdCardUtil;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.model.dto.isolation.personnel.PersonnelExcel;
import com.lnking.lnkingplace.model.dto.isolation.personnel.PersonnelListDto;
import com.lnking.lnkingplace.model.enums.UserType;
import com.lnking.lnkingplace.model.request.isolation.personnel.*;
import com.lnking.lnkingplace.service.IIsolationPersonnelService;
import com.lnking.starter.export.utils.ExportUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangqt
 * @since 2022-08-27
 */
@RestController
@RequestMapping("/isolation-personnel")
@Api(tags = "隔离人员")
public class IsolationPersonnelController {

    @Resource
    IIsolationPersonnelService isolationPersonnelService;

    @Resource
    UserUtil userUtil;

    @ApiOperation("根据身份证更新或新增人员信息")
    @PostMapping("update/Info")
    public ReturnResult updateInfo(@Valid @RequestBody UpdateInfoRequest params){
        isolationPersonnelService.updateInfo(params);
        return ReturnResult.success();
    }

    @ApiOperation("分配房间")
    @PostMapping("distribute/room")
    public ReturnResult distributeRoom(@Valid @RequestBody DistributeRoomRequest params){
        isolationPersonnelService.distributeRoom(params);
        return ReturnResult.success();
    }

    @ApiOperation("扫码枪")
    @GetMapping("get/union/flag")
    public ReturnResult getDataByUnionFlag(@RequestParam String id) {
        return ReturnResult.success(isolationPersonnelService.getDataByUnionFlag(id));
    }

    @ApiOperation("解除隔离状态")
    @PostMapping("update-move")
    public ReturnResult updateMoveStatus(@RequestBody PersonnelUpdateMoveStatus params) {
        isolationPersonnelService.updateMoveStatus(params);
        return ReturnResult.success();
    }

    @ApiOperation("批量修改人员类型或批量修改隔离状态")
    @PostMapping("update-batch-type")
    public ReturnResult updateBatchType(@RequestBody List<UpdateBatchType> data){
        isolationPersonnelService.updateBatchType(data);
        return ReturnResult.success();
    }

    //===============》以上是2期新增或优化接口

    @ApiOperation("扫码填报房间号")
    @PostMapping("updateRoom")
    public ReturnResult updateRoom(@Valid @RequestBody UpdateRoomRequest params){
        isolationPersonnelService.updateRoom(params);
        return ReturnResult.success();
    }

    @ApiOperation(value = "导出隔离解除通知word")
    @GetMapping("export-word")
    public void exportWord(@RequestParam("id") String id, HttpServletResponse response) throws Exception {
        isolationPersonnelService.exportWord(id,response);
    }

    @ApiOperation(value = "导出隔离解除通知pdf")
    @GetMapping("export-pdf")
    public void exportPdf(@RequestParam("id") String id, HttpServletResponse response) throws Exception {
        isolationPersonnelService.exportPdf(id,response);
    }


    @ApiOperation(value = "批量导出隔离解除通知")
    @PostMapping("export-pdf/list")
    public void exportWord( @RequestBody  List<String> ids, HttpServletResponse response) throws Exception {
        isolationPersonnelService.exportPdfs(ids,response);
    }

    @ApiOperation(value = "导出")
    @GetMapping("export")
    public void export(PersonnelSearchRequest params, HttpServletResponse response) throws Exception {
        List<PersonnelListDto> data = (List<PersonnelListDto>)isolationPersonnelService.pageList(params);
        ExportUtils.export(data, response, PersonnelExcel.class);
    }

    @ApiOperation("导入")
    @PostMapping("import")
    public ReturnResult importExcel(String placeId,
                                   @RequestParam("file") MultipartFile file) throws Exception{
        isolationPersonnelService.importExcel(placeId,file);
        return ReturnResult.success();
    }


    @ApiOperation("导入身份证")
    @PostMapping("import/idCard")
    public ReturnResult importExcel(
                                    @RequestParam("file") MultipartFile file) throws Exception{
        isolationPersonnelService.importIdCard(file);
        return ReturnResult.success();
    }

    @ApiOperation("查询列表")
    @GetMapping("list")
    public ReturnResult getList(PersonnelSearchRequest params) {
        String type = userUtil.getUser().getType();
        //隔离场所
        if(type.equals(UserType.PLACE)){
            params.setPlaceName(userUtil.getUser().getName());
        }
//        //
//        else if(type.equals(UserType.SUPERVISE)
//                || type.equals(UserType.LEADER)
//                || type.equals(UserType.INSPECTION)
//                || type.equals(UserType.STREET)){
//            params.setPlaceName(null);
//        }
        return ReturnResult.success(isolationPersonnelService.pageList(params));
    }

    @ApiOperation("查询所有列表")
    @GetMapping("all/list")
    public ReturnResult getAllList(PersonnelSearchRequest params) {
        return ReturnResult.success(isolationPersonnelService.pageList(params));
    }

    @PostMapping("add")
    @ApiOperation("新增")
    public ReturnResult add(@Valid @RequestBody PersonnelAddRequest params) throws Exception{
        if(params.getDocumentType().equals("身份证")){
            IdCardUtil.isValidatedAllIdcard(params.getIdCard());
        }
        return ReturnResult.success(isolationPersonnelService.add(params));
    }

    @PostMapping("update")
    @ApiOperation("修改")
    public ReturnResult update(@Valid @RequestBody PersonnelUpdateRequest params) throws Exception{
        IdCardUtil.isValidatedAllIdcard(params.getIdCard());
        isolationPersonnelService.update(params);
        return ReturnResult.success();
    }

    @ApiOperation("修改审核状态")
    @PostMapping("update-audit")
    public ReturnResult updateAuditStatus(@RequestBody PersonnelUpdateAuditStatus params){
        isolationPersonnelService.updateAuditStatus(params);
        return ReturnResult.success();
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ReturnResult delete(@PathVariable("id") String id) {
        isolationPersonnelService.removeById(id);
        return ReturnResult.success();
    }


    @ApiOperation("查询详情")
    @GetMapping("get-details")
    public ReturnResult getDetails(String id){
        return ReturnResult.success(isolationPersonnelService.getById(id));
    }
}
